/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.firstdemo.ui.theme

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.platform.LocalContext


data class EColors(
    val primary: Primary = Primary(),
    val success: Success = Success(),
    val warning: Warning = Warning(),
    val danger: Danger = Danger(),
    val info: Info = Info(),
    val text: Text = Text(),
    val border: Border = Border(),
    val fill: Fill = Fill(),
    val background: Background = Background(),
    val basic: Basic = Basic
)

private val eColorsLight = EColors(
    primary = primaryLight,
    success = successLight,
    warning = warningLight,
    danger = dangerLight,
    info = infoLight,
    text = textLight,
    border = borderLight,
    fill = fillLight,
    background = backgroundLight,
)

private val eColorsDark = EColors(
    primary = primaryDark,
    success = successDark,
    warning = warningDark,
    danger = dangerDark,
    info = infoDark,
    text = textDark,
    border = borderDark,
    fill = fillDark,
    background = backgroundDark,
)

val LocalEColors = staticCompositionLocalOf<EColors> {
    EColors()
}

private val lightScheme = lightColorScheme(
    primary = primaryLight.base,
    onPrimary = Basic.white,
    primaryContainer = primaryLight.light9,
    onPrimaryContainer = primaryLight.base,
    primaryFixed = primaryLight.light9,
    onPrimaryFixed = primaryLight.base,
    primaryFixedDim = primaryLight.light7,
    onPrimaryFixedVariant = primaryLight.base,

    secondary = primaryLight.base,
    onSecondary = Basic.white,
    secondaryContainer = primaryLight.light9,
    onSecondaryContainer = primaryLight.base,
    secondaryFixed = primaryLight.light9,
    onSecondaryFixed = primaryLight.base,
    secondaryFixedDim = primaryLight.light7,
    onSecondaryFixedVariant = primaryLight.base,

    tertiary = primaryLight.base,
    onTertiary = Basic.white,
    tertiaryContainer = primaryLight.light9,
    onTertiaryContainer = primaryLight.base,
    tertiaryFixed = primaryLight.light9,
    onTertiaryFixed = primaryLight.base,
    tertiaryFixedDim = primaryLight.light7,
    onTertiaryFixedVariant = primaryLight.base,

    surface = backgroundLight.page,
    background = backgroundLight.page,
    surfaceContainer = backgroundLight.base,

    surfaceContainerHighest = backgroundLight.overlay
)
private val darkScheme = darkColorScheme(
    primary = primaryDark.base,
    onPrimary = Basic.black,
    primaryContainer = primaryDark.light9,
    onPrimaryContainer = primaryDark.base,
    primaryFixed = primaryDark.light9,
    onPrimaryFixed = primaryDark.base,
    primaryFixedDim = primaryDark.light7,
    onPrimaryFixedVariant = primaryDark.base,

    secondary = primaryDark.base,
    onSecondary = Basic.black,
    secondaryContainer = primaryDark.light9,
    onSecondaryContainer = primaryDark.base,
    secondaryFixed = primaryDark.light9,
    onSecondaryFixed = primaryDark.base,
    secondaryFixedDim = primaryDark.light7,
    onSecondaryFixedVariant = primaryDark.base,

    tertiary = primaryDark.base,
    onTertiary = Basic.black,
    tertiaryContainer = primaryDark.light9,
    onTertiaryContainer = primaryDark.base,
    tertiaryFixed = primaryDark.light9,
    onTertiaryFixed = primaryDark.base,
    tertiaryFixedDim = primaryDark.light7,
    onTertiaryFixedVariant = primaryDark.base,

    surface = backgroundDark.page,
    background = backgroundDark.page,
    surfaceContainer = backgroundDark.base,

    surfaceContainerHighest = backgroundDark.overlay
)


@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false,
    content: @Composable() () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> darkScheme
        else -> lightScheme
    }

    val eColor = if (darkTheme) eColorsDark else eColorsLight
    val typography = if (darkTheme) typographyLight else typographyDark

//    val view = LocalView.current
//    if (!view.isInEditMode) {
//        SideEffect {
//            val window = (view.context as Activity).window
//            window.statusBarColor = colorScheme.primary.toArgb()
//            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
//        }
//    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = typography,
        shapes = shapes,
    ) {
        CompositionLocalProvider(
            LocalEColors provides eColor,
            LocalTextStyle provides typography.bodyMedium,
            content = content
        )
    }
}

val MaterialTheme.eColors: EColors
    @Composable
    @ReadOnlyComposable
    get() = LocalEColors.current